#include "nstar_adt_httputil.h"
#include "nstar_adt_http_page.h"
#include "ebm_task.h"
#include "ebm_env.h"
#include "user_cfg.h"

#define JS_P1_E1 		"ssid"
#define JS_P1_E2 		"ssip"
#define JS_P1_E3 		"backip"
#define JS_P1_E4 	    "otaip"
#define JS_P1_E5 	    "lip"
#define JS_P1_E6 	    "lmask"
#define JS_P1_E7 	    "lgw"
#define JS_P1_E8 	    "mac"
#define JS_P1_E9 	    "spgip"
#define JS_P1_EX10 	    "rsip"
#define JS_P1_EX11 	    "rpgip"
#define JS_P1_EX12 	    "rsid"




#define C_PAGE_NAME HTML_PAGE2_NAME

#if 0
"<p><label>本地IP:</label><input type='text' size='26' id='"JS_P1_E5"' name='"JS_P1_E5"'/></p>"\
"<p><label>本地掩码:</label><input type='text' size='26' id='"JS_P1_E6"' name='"JS_P1_E6"'/></p>"\
"<p><label>本地网关:</label><input type='text' size='26' id='"JS_P1_E7"' name='"JS_P1_E7"'/></p>"
#endif

#define C_PAGE_BODY "<style>label1{display: inline-block;}</style>"\
"<body>"\
"<ul>"\
"<li><a class='home'>STAR-斯达鑫辉</a></li>"\
"<li><a href='"HTML_PAGE1_NAME".html'>参数状态</a></li>"\
"<li><a href='"HTML_PAGE2_NAME".html' class='active'>网络设置</a></li>"\
"<li><a href='"HTML_PAGE3_NAME".html'>音频设置</a></li>"\
"<li><a href='"HTML_PAGE4_NAME".html'>信息日志</a></li>"\
"<li><a href='"HTML_PAGE5_NAME".html'>固件升级</a></li>"\
"</ul>"\
"<h2> chengdu sida web management interface </h2>"\
"<h3> 网络设置</h3>"\
"<div style='background:snow; display:block; padding:10px 10%;'>"\
"<form id='frmSetting' method='POST' action='"C_PAGE_NAME".cgi'>"\
"<p><label>本地IP:</label>\
<label1 style='padding:0px 8px 0px 0px;'>IP地址:</label1><input type='text' size='11' id='"JS_P1_E5"' name='"JS_P1_E5"'/>\
<label1 style='padding:0px 8px 0px 20px;'>子网掩码:</label1><input type='text' size='11' id='"JS_P1_E6"' name='"JS_P1_E6"'/>\
<label1 style='padding:0px 8px 0px 20px;'>默认网关:</label1><input type='text' size='11' id='"JS_P1_E7"' name='"JS_P1_E7"'/></p>"\
"<p><label>接收资源编码:</label><input type='text' size='26' id='"JS_P1_EX12"' name='"JS_P1_EX12"'/></p>"\
"<p><label>输出逻辑编码:</label><input type='text' size='26' id='"JS_P1_E1"' name='"JS_P1_E1"'/></p>"\
"<p><label>接收指令IP:</label><input type='text' size='26' id='"JS_P1_EX10"' name='"JS_P1_EX10"'/></p>"\
"<p><label>接收节目IP:</label><input type='text' size='26' id='"JS_P1_EX11"' name='"JS_P1_EX11"'/></p>"\
"<p><label>输出指令IP:</label><input type='text' size='26' id='"JS_P1_E2"' name='"JS_P1_E2"'/></p>"\
"<p><label>输出节目IP:</label><input type='text' size='26' id='"JS_P1_E9"' name='"JS_P1_E9"'/></p>"\
"<p><label>回传地址:</label><input type='text' size='26' id='"JS_P1_E3"' name='"JS_P1_E3"'/></p>"\
"<p><label>ota服务器IP:</label><input type='text' size='26' id='"JS_P1_E4"' name='"JS_P1_E4"'/></p>"\
"<p><label>MAC地址:</label><input type='text' size='26' disabled='disabled' id='"JS_P1_E8"' name='"JS_P1_E8"'/></p>"\
"<p><p><input type='button' value='保存' onclick ='commit();'  id='rcorners1'/>"\
"<input type='button' onclick ='savereboot();' value='重启终端' id='rcorners1' style='background: #ff6300'/></p>"\
"</form>"\
"</div>"\
"</body>"\
"</html>"


#define C_PAGE_BODY_2 "<style>label1{display: inline-block;}</style>"\
"<body>"\
"<ul>"\
"<li><a class='home'>STAR-斯达鑫辉</a></li>"\
"<li><a href='"HTML_PAGE1_NAME".html'>参数状态</a></li>"\
"<li><a href='"HTML_PAGE2_NAME".html' class='active'>网络设置</a></li>"\
"<li><a href='"HTML_PAGE3_NAME".html'>音频设置</a></li>"\
"<li><a href='"HTML_PAGE4_NAME".html'>信息日志</a></li>"\
"<li><a href='"HTML_PAGE5_NAME".html'>固件升级</a></li>"\
"</ul>"\
"<h2> chengdu sida web management interface </h2>"\
"<h3> 网络设置</h3>"\
"<div style='background:snow; display:block; padding:10px 10%;'>"\
"<form id='frmSetting' method='POST' action='"C_PAGE_NAME".cgi'>"\
"<p><label>本地IP:</label>\
<label1 style='padding:0px 8px 0px 0px;'>IP地址:</label1><input type='text' size='11' disabled='disabled' id='"JS_P1_E5"' name='"JS_P1_E5"'/>\
<label1 style='padding:0px 8px 0px 20px;'>子网掩码:</label1><input type='text' size='11' disabled='disabled' id='"JS_P1_E6"' name='"JS_P1_E6"'/>\
<label1 style='padding:0px 8px 0px 20px;'>默认网关:</label1><input type='text' size='11' disabled='disabled' id='"JS_P1_E7"' name='"JS_P1_E7"'/></p>"\
"<p><label>接收资源编码:</label><input type='text' size='26' id='"JS_P1_EX12"' name='"JS_P1_EX12"'/></p>"\
"<p><label>输出逻辑编码:</label><input type='text' size='26' id='"JS_P1_E1"' name='"JS_P1_E1"'/></p>"\
"<p><label>接收指令IP:</label><input type='text' size='26' id='"JS_P1_EX10"' name='"JS_P1_EX10"'/></p>"\
"<p><label>接收节目IP:</label><input type='text' size='26' id='"JS_P1_EX11"' name='"JS_P1_EX11"'/></p>"\
"<p><label>输出指令IP:</label><input type='text' size='26' id='"JS_P1_E2"' name='"JS_P1_E2"'/></p>"\
"<p><label>输出节目IP:</label><input type='text' size='26' id='"JS_P1_E9"' name='"JS_P1_E9"'/></p>"\
"<p><label>回传地址:</label><input type='text' size='26' id='"JS_P1_E3"' name='"JS_P1_E3"'/></p>"\
"<p><label>ota服务器IP:</label><input type='text' size='26' id='"JS_P1_E4"' name='"JS_P1_E4"'/></p>"\
"<p><label>MAC地址:</label><input type='text' size='26' disabled='disabled' id='"JS_P1_E8"' name='"JS_P1_E8"'/></p>"\
"<p><p><input type='button' value='保存' onclick ='commit();'  id='rcorners1'/>"\
"<input type='button' onclick ='savereboot();' value='重启终端' id='rcorners1' style='background: #ff6300'/></p>"\
"</form>"\
"</div>"\
"</body>"\
"</html>"




#define MIN(a,b) ( ((a)>=(b))?(b):(a))
#define MAX(a,b) ( ((a)>=(b))?(a):(b))

extern unsigned char syscfg_ota_pid_set(unsigned short val);
extern unsigned short syscfg_ota_pid_get(void);

static unsigned char updata_lip_flg=0;
static unsigned char _modify_local_ip(const char* p_dta)
{
    return usercfg_str_local_ip(p_dta);
}

static unsigned char _modify_local_mask(const char* p_dta)
{
    return usercfg_str_mask(p_dta);
}

static unsigned char _modify_local_gw(const char* p_dta)
{
    return usercfg_str_gateway(p_dta);
}




static unsigned char _modify_ip(char* pdata, unsigned char (*func)(unsigned char*, unsigned short))
{
    unsigned short port=0;
	unsigned char ip[4];
	char *p_port;
	if(0 == str2ip(pdata, ip, MIN(16, strlen(pdata)+1))){
		p_port= strstr(pdata, ":");
		if(NULL != p_port){
			p_port+=1;
			port= atoi(p_port);
		}
	}
    myarg(ip[0]);
    myarg(port);
	if(ip[0] > 0 && port >0){
        func(ip, port);
	}
    return 0;

}

//static unsigned char _modify_adpSip(const char* p_dta)
//{
/*指令通道*/
//extern void command_net_restart(void);

//	unsigned short port=0;
//	unsigned char ip[4], ret=0;
//	char *p_port;
//	if(0 == str2ip(p_dta, ip, MIN(16, strlen(p_dta)+1))){
//		p_port= strstr(p_dta, ":");
//		if(NULL != p_port){
//			p_port+=1;
//			port= atoi(p_port);
//			ret= 1;
//		}
//	}

//	if(ip[0] > 0 && port >0){
//		adpEnv_set_platip(ip, port);
//	}
//	return ret;
//}

static unsigned char _modify_adpSip(char *p_dta) {
    return _modify_ip(p_dta, adpEnv_set_platip);
}

static unsigned char _modify_ebmPassback_ip(char *p_dta) {
    return _modify_ip(p_dta, adpEnv_set_backip);
}

static unsigned char _modify_adpPg_ip(char *p_dta) {
    return _modify_ip(p_dta, adpEnv_set_pgip);
}


static unsigned char _modify_ota_ip(char *p_dta)
{

/*升级通道*/
	unsigned short port=0;
	unsigned char ip[4], ret=0;
	char *p_port;
	if(0 == str2ip(p_dta, ip, MIN(16, strlen(p_dta)+1))){
		p_port= strstr(p_dta, ":");
		if(NULL != p_port){
			p_port+=1;
			port= atoi(p_port);
			ret= 1;
		}
	}
	if(ip[0] > 0 && port >0){
		ret = syscfg_ota_sip_set(ip);
		ret |= syscfg_ota_port_set(port);
		if(ret != 0){
			//ota_client_weakup();
		}
	}
	return ret;
}


static unsigned char _modify_adpLogic(unsigned char*p_dta)
{
#define LEN 6

	unsigned char buf[LEN];
	unsigned char i;
	unsigned char ret = 0;

	for(i=0;i<LEN;i++){
		if( (buf[i] = nstar_cmm_canver_get_hex_dec_byte(p_dta)) ==  0xff ){
			break;
		}
		p_dta += 2;
	}
	if(i == LEN ){
		adpEnv_set_logicID(buf);
	}

	return ret;
}


static unsigned char _modify_ebmLogic(unsigned char*p_dta)
{
#define LEN 6

	unsigned char buf[LEN];
	unsigned char i;
	unsigned char ret = 0;

	for(i=0;i<LEN;i++){
		if( (buf[i] = nstar_cmm_canver_get_hex_dec_byte(p_dta)) ==  0xff ){
			break;
		}
		p_dta += 2;
	}
	if(i == LEN ){
		ebmEnv_set_logicID(buf);
	}

	return ret;
}


static unsigned char _modify_ebmSip(char *p_dta) {
    return _modify_ip(p_dta, ebmEnv_set_platip);
}

static unsigned char _modify_ebmPg_ip(char *p_dta) {
    return _modify_ip(p_dta, ebmEnv_set_pgip);
}




#define IPV4_FMT        "'%s':'%d.%d.%d.%d',"
#define IPV4_PROT_FMT   "'%s':'%d.%d.%d.%d:%02d',"
#define IPV4_ID_FMT(ip4)     (ip4>>24)&0xff,(ip4>>16)&0xff,(ip4>>8)&0xff,ip4&0xff

static void _add_htm_element(unsigned char mode)
{

#define usercfg_str_mask(str)    _set_ip(str,p_wcfg->mask)
#define usercfg_str_gateway(str) _set_ip(str,p_wcfg->gateway)
#define usercfg_str_PSip(str)    _set_ip(str,p_wcfg->PSip)

	unsigned int sip,dip;
    const unsigned char *lip, *mask, *gw, *otaip;
	unsigned short sport,dport,otaport;
	unsigned char *id;
	if(mode == URI_REPOS_HTML){
		REQUST_JSCRIPT_ELEMENT(JS_P1_E1);
		REQUST_JSCRIPT_ELEMENT(JS_P1_E2);
		REQUST_JSCRIPT_ELEMENT(JS_P1_E3);
		REQUST_JSCRIPT_ELEMENT(JS_P1_E4);
		REQUST_JSCRIPT_ELEMENT(JS_P1_E5);
		REQUST_JSCRIPT_ELEMENT(JS_P1_E6);
		REQUST_JSCRIPT_ELEMENT(JS_P1_E7);
		REQUST_JSCRIPT_ELEMENT(JS_P1_E8);
        REQUST_JSCRIPT_ELEMENT(JS_P1_E9);
		REQUST_JSCRIPT_ELEMENT(JS_P1_EX10);
        REQUST_JSCRIPT_ELEMENT(JS_P1_EX11);
        REQUST_JSCRIPT_ELEMENT(JS_P1_EX12);
	}
	else if(mode == URI_REPOS_JSON){
		sip= adpEnv_get_sip();
		sport= adpEnv_get_plat_port();
		dip= adpEnv_get_passback_ip();
		dport= adpEnv_get_passback_port();
		otaip = syscfg_ota_sip_get();
		otaport= syscfg_ota_port_get();
		lip= p_readcfg->local_ip;
		mask= p_readcfg->mask;
		gw= p_readcfg->gateway;
        id = p_readcfg->adp.logic_id;  
		http_sprintf("'%s':'%02X%02X%02X%02X%02X%02X',",JS_P1_E1, id[0],id[1],id[2],id[3],id[4],id[5]); 
        http_sprintf(IPV4_PROT_FMT,JS_P1_E2,(sip>>24)&0xff,(sip>>16)&0xff,(sip>>8)&0xff,(sip)&0xff,sport);
		http_sprintf(IPV4_PROT_FMT,JS_P1_E3,(dip>>24)&0xff,(dip>>16)&0xff,(dip>>8)&0xff,(dip)&0xff,dport);
		http_sprintf(IPV4_PROT_FMT,JS_P1_E4, otaip[0], otaip[1], otaip[2], otaip[3], otaport);
		http_sprintf(IPV4_FMT,JS_P1_E5,lip[0], lip[1], lip[2],lip[3]);
		http_sprintf(IPV4_FMT,JS_P1_E6, mask[0], mask[1], mask[2],mask[3]);
		http_sprintf(IPV4_FMT,JS_P1_E7, gw[0], gw[1], gw[2],gw[3]);
        sip = p_readcfg->adp.pg_ip;
        http_sprintf(IPV4_PROT_FMT,JS_P1_E9, IPV4_ID_FMT(sip), p_readcfg->adp.pg_port);

        sip = p_readcfg->ebm.plat_ip;
        http_sprintf(IPV4_PROT_FMT,JS_P1_EX10, IPV4_ID_FMT(sip), p_readcfg->ebm.plat_port);

        sip = p_readcfg->ebm.pg_ip;
        http_sprintf(IPV4_PROT_FMT,JS_P1_EX11, IPV4_ID_FMT(sip), p_readcfg->ebm.pg_port);
        
        id = p_readcfg->ebm.logic_id;  
        http_sprintf("'%s':'%02X%02X%02X%02X%02X%02X',",JS_P1_EX12, id[0],id[1],id[2],id[3],id[4],id[5]); 

        
		//sys_status_pyhsics_addr_get(id);
		//syscfg_read_macaddr(id, 6);
		http_sprintf("'%s':'%02X-%02X-%02X-%02X-%02X-%02X',",JS_P1_E8, id[0],id[1],id[2],id[3],id[4],id[5] );
	}
}



void parm2_pos_htm(unsigned char mode)
{
	if(mode == URI_REPOS_HTML){
		if(1 != user_right_get())
			http_page_htm(C_PAGE_NAME, C_PAGE_BODY_2, _add_htm_element);
		else
			http_page_htm(C_PAGE_NAME, C_PAGE_BODY, _add_htm_element);
	}
	else if(mode == URI_REPOS_JSON)
		http_page_json(C_PAGE_NAME, _add_htm_element);
}

void parm2_rpos_cgi(char *url)
{ 
	char *p_content; 
	p_content = (char*)my_get_param_url(url);
	if(NULL == p_content){
		return;
	}
    http_handle_parm(p_content, JS_P1_E1, (void*)_modify_adpLogic);
	http_handle_parm(p_content, JS_P1_E2, (void*)_modify_adpSip);
    http_handle_parm(p_content, JS_P1_E9, (void*)_modify_adpPg_ip);
	http_handle_parm(p_content, JS_P1_E4, (void*)_modify_ota_ip);

	if(1 == user_right_get()){	
		http_handle_parm(p_content, JS_P1_E5, (void*)_modify_local_ip);
		http_handle_parm(p_content, JS_P1_E6, (void*)_modify_local_mask);
		http_handle_parm(p_content, JS_P1_E7, (void*)_modify_local_gw);
	}
    
    http_handle_parm(p_content, JS_P1_E3, (void*)_modify_ebmPassback_ip);
    http_handle_parm(p_content, JS_P1_EX10, (void*)_modify_ebmSip);
    http_handle_parm(p_content, JS_P1_EX11, (void*)_modify_ebmPg_ip);
    http_handle_parm(p_content, JS_P1_EX12, (void*)_modify_ebmLogic);

    
	http_sprintf_init();
	http_sprintf(HTML_CGI_JUMP, LOCAL_IP, C_PAGE_NAME);
	http_sprintf_send();

#if 1
	if(updata_lip_flg)
		nstar_update_local_ip();
	updata_lip_flg=0;
#endif
	
}

char *web_new_locl_ip(void)
{
	const unsigned char *lip;
	static char strlip[20];
	//lip= sys_status_local_ip_get();
	lip= p_readcfg->local_ip;
	sprintf(strlip, "%d.%d.%d.%d", lip[0], lip[1], lip[2],lip[3]);
	return strlip;
}

void rpos_reboot_cgi(void)
{ 
	http_sprintf_init();
	http_sprintf(HTML_CGI_JUMP, LOCAL_IP, C_PAGE_NAME);
	http_sprintf_send();
	user_reboot(3);
}


